import { setLocale } from '@/i18n'

export default (to, from, next) => {
  if (to.query.lang) {
    setLocale(to.query.lang).then((realLang) => {
      next({ name: to.name, params: { ...to.params, lang: realLang } })
    })
  } else if (to.params.lang) {
    setLocale(to.params.lang).then((realLang) => {
      if (to.params.lang === realLang) {
        next()
      } else {
        next({ name: to.name, params: { ...to.params, lang: realLang } })
      }
    })
  } else {
    next()
  }
}
